﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/

using System;
using ZFine.Application.SystemManage;
using ZFine.Code;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Configuration;
using ZFine.Domain.Entity;

namespace ZFine.Web.Controllers
{
    [HandlerLogin]
    public class ClientsDataController : Controller
    {
        private ItemsApp itemsapp = new ItemsApp();
        private ItemsDetailApp itemsdetailapp = new ItemsDetailApp();
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetClientsDataJson()
        {
            var data = new
            {
                dataItems = this.GetDataItemList(),
                organize = this.GetOrganizeList(),
                role = this.GetRoleList(),
                duty = this.GetDutyList(),
                user = "",
                authorizeMenu = this.GetMenuList(),
                authorizeButton = this.GetMenuButtonList(),
                newType=this.GetNewsType(),
                DateDic=this.GetDicObj()
            };
            return Content(data.ToJson());
        }

        private object GetDicObj()
        {
            Dictionary<string, object> dictionaryItem = new Dictionary<string, object>();
            string dicliststring = ConfigurationManager.AppSettings["AllParameter"];
            if (!string.IsNullOrEmpty(dicliststring)) {
                string[] diclist = dicliststring.Split(',');
                if (diclist.Length > 0) {
                    for (int i = 0; i < diclist.Length; i++)
                    {
                        string dic = ConfigurationManager.AppSettings[diclist[i]];
                        dictionaryItem.Add(dic, GetAllTreeSelectJson(dic));
                    }
                }
            }
            return dictionaryItem;
        }

        private object GetNewsType()
        {
            Dictionary<string, object> dictionaryItem = new Dictionary<string, object>();
            var itemdata = new ItemsApp().GetList("新闻分类");
            if (itemdata != null && itemdata.Count>0)
            {
                var childrenlist = new ItemsApp().GetList("", itemdata[0].F_Id);
                if (childrenlist != null && childrenlist.Count > 0) {
                    foreach (var item in childrenlist) {
                        dictionaryItem.Add(item.F_EnCode, item.F_FullName);
                    }
                }
            }
            return dictionaryItem;
        }
        public object GetAllParameter() 
        {
            Dictionary<string, object> dictionaryItem = new Dictionary<string, object>();
            var itemdata = new ItemsDetailApp().GetList();

            return dictionaryItem;
        }

        public object GetAllTreeSelectJson(string Types)
        {
            var thisitem = itemsapp.GetFormByCode(Types);
            if (thisitem != null)
            {
                var dataresult = itemsdetailapp.GetList(thisitem.F_Id);
                var treeList = new List<object>();
                foreach (ItemsDetailEntity item in dataresult)
                {
                    treeList.Add(new { id = item.F_Id, name = item.F_ItemName });
                }
                return treeList;
            }
            return null;
        }

        private object GetDataItemList()
        {
            try
            {
            var itemdata = new ItemsDetailApp().GetList();
            Dictionary<string, object> dictionaryItem = new Dictionary<string, object>();
            foreach (var item in new ItemsApp().GetList())
            {
                var dataItemList = itemdata.FindAll(t => t.F_ItemId.Equals(item.F_Id));
                Dictionary<string, string> dictionaryItemList = new Dictionary<string, string>();
                foreach (var itemList in dataItemList)
                {
                    dictionaryItemList.Add(itemList.F_ItemCode, itemList.F_ItemName);
                }
                dictionaryItem.Add(item.F_EnCode, dictionaryItemList);
            }
            return dictionaryItem;
        }
            catch (Exception ex)
            {
                throw;
            }
          
        }
        private object GetOrganizeList()
        {
            OrganizeApp organizeApp = new OrganizeApp();
            var data = organizeApp.GetList();
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            foreach (OrganizeEntity item in data)
            {
                var fieldItem = new
                {
                    encode = item.F_EnCode,
                    fullname = item.F_FullName
                };
                dictionary.Add(item.F_Id, fieldItem);
            }
            return dictionary;
        }
        private object GetRoleList()
        {
            RoleApp roleApp = new RoleApp();
            var data = roleApp.GetList();
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            foreach (RoleEntity item in data)
            {
                var fieldItem = new
                {
                    encode = item.F_EnCode,
                    fullname = item.F_FullName
                };
                dictionary.Add(item.F_Id, fieldItem);
            }
            return dictionary;
        }
        private object GetDutyList()
        {
            DutyApp dutyApp = new DutyApp();
            var data = dutyApp.GetList();
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            foreach (RoleEntity item in data)
            {
                var fieldItem = new
                {
                    encode = item.F_EnCode,
                    fullname = item.F_FullName
                };
                dictionary.Add(item.F_Id, fieldItem);
            }
            return dictionary;
        }
        private object GetMenuList()
        {
            var roleId = OperatorProvider.Provider.GetCurrent().RoleId;
            return ToMenuJson(new RoleAuthorizeApp().GetMenuList(roleId), "0");
        }
        private string ToMenuJson(List<ModuleEntity> data, string parentId)
        {
            StringBuilder sbJson = new StringBuilder();
            sbJson.Append("[");
            List<ModuleEntity> entitys = data.FindAll(t => t.F_ParentId == parentId);
            if (entitys.Count > 0)
            {
                foreach (var item in entitys)
                {
                    string strJson = item.ToJson();
                    strJson = strJson.Insert(strJson.Length - 1, ",\"ChildNodes\":" + ToMenuJson(data, item.F_Id) + "");
                    sbJson.Append(strJson + ",");
                }
                sbJson = sbJson.Remove(sbJson.Length - 1, 1);
            }
            sbJson.Append("]");
            return sbJson.ToString();
        }
        private object GetMenuButtonList()
        {
            var roleId = OperatorProvider.Provider.GetCurrent().RoleId;
            var data = new RoleAuthorizeApp().GetButtonList(roleId);
            var dataModuleId = data.Distinct(new ExtList<ModuleButtonEntity>("F_ModuleId"));
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            foreach (ModuleButtonEntity item in dataModuleId)
            {
                var buttonList = data.Where(t => t.F_ModuleId.Equals(item.F_ModuleId));
                dictionary.Add(item.F_ModuleId, buttonList);
            }
            return dictionary;
        }
    }
}
